import './assets/js/UUID.js';
import verify from './assets/js/Verify.js';
import Android from './android/index';
import Vue from 'vue'
import VueBus from './plugins/VueBus';
import Filters from './plugins/Filters';
import Directive from './plugins/Directive';
import Message from './plugins/Message.js';
import App from './App.vue';
import router from './router/index';
import store from './store/index';
import vuetify from './plugins/Vuetify';
import Ajax from './plugins/Ajax.js';
import Loading from './plugins/Loading.js';
import Alert from './plugins/Alert.js';
import './plugins/GlobalComponents.js';
import echarts from 'echarts';
import './assets/iconfont/iconfont.css';

Vue.prototype.$echarts = echarts;
Vue.prototype.verify = verify;
Vue.use(Android);
Vue.use(VueBus);
Vue.use(Ajax);
Vue.use(Filters);
Vue.use(Directive);
Vue.use(Message);
Vue.use(Alert);
Vue.use(Loading);

Vue.config.productionTip = false;

new Vue({
  router,
  store,
  vuetify,
  beforeCreate() {
    //加载当前用户信息
    this.$store.dispatch('loadUserContext', this);
    //加载当前用户权限
    this.$store.dispatch('loadNavMenu', this);
  },
  mounted() {
    //会话失败事件监听
    this.$bus.$on(this.$env.TOKEN_INVALID, () => {
      this.logout();
    });

    //登录退出事件监听
    this.$bus.$on(this.$env.LOGIN_OUT_SUCCESS, () => {
      this.logout();
    });

    //登录成功事件监听
    this.$bus.$on(this.$env.LOGIN_SUCCESS, loginUser => {
      this.login(loginUser);
    });

  },
  destroyed() {
    this.$bus.$off(this.$env.TOKEN_INVALID);
    this.$bus.$off(this.$env.LOGIN_OUT_SUCCESS);
    this.$bus.$off(this.$env.LOGIN_SUCCESS);
  },
  methods: {
    /**
     * 登出处理
     * @author Canaan
     * @date 2019/3/15 10:56
     */
    logout() {
      this.$store.commit('userContext', {});
      this.$store.commit('clearNavMenu');
      sessionStorage.removeItem(this.$env.LOGIN_TOKEN);
      sessionStorage.removeItem(this.$env.LOGIN_ROLE);
      if (this.$route.path !== "/login") {
        this.$router.push('/login')
      }
    },
    /**
     * 登录处理
     * @author Canaan
     * @date 2019/3/15 10:57
     */
    login(loginUser) {
      sessionStorage.setItem(this.$env.LOGIN_TOKEN, loginUser.token);
      if (loginUser.rememberMe) {
        localStorage.setItem(this.$env.LOGIN_USER, loginUser.username);
      } else {
        localStorage.removeItem(this.$env.LOGIN_USER);
      }

      Promise.all([
        //加载当前用户信息
        this.$store.dispatch('loadUserContext', this)
          .then(user => {
            if (!user || !user.role) {
              return;
            }
            //允许当前访问用户的角色
            const permission = ["STORE", "BRAND"];
            if (permission.indexOf(user.role) <= -1) {
              this.$message.warning("您没有权限访问");
              this.logout();
              return;
            }

            sessionStorage.setItem(this.$env.LOGIN_ROLE, user.role);
          }),

        //加载用户功能模块(viewWidget)
        this.$store.dispatch("loadNavMenu", this)

      ]).then(res => {
        //加载当前用户模块 /当前用户信息
        this.$router.push('/');
      }).catch(res => {
        this.$message.error("登录失败");
      })

    },
  },
  render: h => h(App)
}).$mount('#app');
